package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 查出所有的章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", courseId);
        List<EduChapter> chapterChapter = baseMapper.selectList(chapterWrapper);

        // 查出所有的小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        List<EduVideo> videoChapter = videoService.list(videoWrapper);

        // 封装章节和小节
        List<ChapterVo> list = new ArrayList<>();
        chapterChapter.forEach(chapter -> {
            String id = chapter.getId();
            String title = chapter.getTitle();
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(id);
            chapterVo.setTitle(title);
            videoChapter.forEach(video -> {
                if (Objects.equals(courseId, video.getCourseId()) &&
                        Objects.equals(id, video.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(video.getId());
                    videoVo.setTitle(video.getTitle());
                    videoVo.setVideoSourceId(video.getVideoSourceId());
                    chapterVo.getChildren().add(videoVo);
                }
            });
            list.add(chapterVo);
        });
        return list;
    }

    /**
     * 删除章节信息，如果章节下面有小节，则不能够删除
     *
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {

        // 根据chapterId查询小节表，如何查询到数据，则不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        // 查询符合条件的记录数
        int count = videoService.count(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "该章节下存在多个小节，不能进行删除");
        } else {
            // 删除章节
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    /**
     * 根据课程id删除章节
     *
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
